package com.tencent.trtc.apiexample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import static com.tencent.trtc.apiexample.LoginActivity.USERNAME;

public class mAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mLayoutInfalter;
    //用ArrayList来存储数据，新增数据
    private List<RoomInfo> mRoomInfos = new ArrayList<>();

    //构造函数，初始化
    public mAdapter(Context context, ArrayList<RoomInfo> RoomInfos){
        this.mContext = context;
        this.mRoomInfos = RoomInfos;
        mLayoutInfalter = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return mRoomInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mRoomInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder{
       public TextView roomIDTextView,nameTextView,createrTextView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //返回一个视图，convertView 就是找到要绑定的视图，LayoutInfalter的主要功能就是解析View
        // 将Layout从xml文件从解析出来，解析成View，一个视图
        ViewHolder holder = null;
        if(convertView == null) {
            convertView = mLayoutInfalter.inflate(R.layout.activity_list,null);
            holder = new ViewHolder();
            holder.roomIDTextView=(TextView) convertView.findViewById(R.id.tv_2);
            holder.nameTextView= (TextView) convertView.findViewById(R.id.tv_1);
            holder.createrTextView=(TextView) convertView.findViewById(R.id.tv_3);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.nameTextView.setText(mRoomInfos.get(position).getRoomName()); //设置文本
        holder.roomIDTextView.setText(String.valueOf(mRoomInfos.get(position).getiD()));
        holder.createrTextView.setText(mRoomInfos.get(position).getCreater());

        return convertView;
    }
}
